/*
 * Creation : 19/09/2013
 */
/**
 * 
 */
package com.japcatalog.dao.impl;

import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.japcatalog.dao.CatalogDao;
import com.japcatalog.model.Catalog;
import com.japcatalog.util.HibernateUtil;

/**
 * The Class CatalogDaoImpl.
 * 
 * @author Fernando Ariel Perelli
 */
public class CatalogDaoImpl implements CatalogDao {

	/** The session. */
	private Session session;

	/**
	 * Getter session.
	 * 
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}

	/**
	 * Sets the session.
	 * 
	 * @param session
	 *            the session to set
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Catalog> findAll() {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		Collection<Catalog> catalogs = null;
		try {
			transaction = this.session.beginTransaction();
			catalogs = this.session.createCriteria(Catalog.class).list();
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}
		return catalogs;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#findById(java.lang.Long)
	 */
	@Override
	public Catalog findById(Long id) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		Catalog catalog = null;
		try {
			transaction = this.session.beginTransaction();
			catalog = (Catalog) this.session.get(Catalog.class, id);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}

		return catalog;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#create(com.japcatalog.model.Catalog)
	 */
	@Override
	public Long create(Catalog catalog) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		Long id = null;
		try {
			transaction = this.session.beginTransaction();
			id = (Long) this.session.save(catalog);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
		return id;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#update(com.japcatalog.model.Catalog)
	 */
	@Override
	public void update(Catalog catalog) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.update(catalog);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#delete(com.japcatalog.model.Catalog)
	 */
	@Override
	public void delete(Catalog catalog) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.delete(catalog);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.CatalogDao#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Catalog catalog = this.findById(id);
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.delete(catalog);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
	}

}
